#include <iostream>
#include <cstring>
using namespace std;
#define maxn (2*105)

int n;
int e[maxn];
int dp[maxn][maxn];

//dfs()中e[l]*e[i+1]*e[r+1]值得深思
int dfs(int l, int r){
	if(l==r-1) return e[l]*e[r]*e[r+1];
	if(l==r) return 0;
	if(dp[l][r]>=0) return dp[l][r];
	int ret=0;
	for(int i=l; i<r; i++){
		ret = max(ret, dfs(l,i)+dfs(i+1,r)+e[l]*e[i+1]*e[r+1]);
	}
	return dp[l][r] = ret;
}

int main(){
	memset(dp, -1, sizeof(dp));
	cin>>n;
	for(int i=0; i<n; i++){
		cin>>e[i];
		e[i+n]=e[i];
	}
	
	int maxans = 0;
	for(int l=0; l<n; l++){
		int r=l+n-1;
		maxans = max(maxans, dfs(l, r));
	}
	cout << maxans << endl;
	
	return 0;
}